<script>
  import wxSdk from './utils/wx'

  export default {
    async onLaunch() {
      console.log('app created')
      wxSdk.init();
      App.systemInfo = wx.getSystemInfoSync();
      App.login = async function () {
        const { code } = await wx.login();
        App.wxCode = code;
        const result = await wx.request({
          url: '/emp/login/login/wxCode',
          data: {
            wxCode: code
          }
        })
        console.info(result)
        if (result.code === 200) {
          App.loginInfo = result.data;
          const userResult = await wx.request({
            url: '/bg/sys/account'
          });
          if (userResult.code === 200) {
            let orgId = 0;
            userResult.data.orgNames.some(org => {
              if(org.isLeader){
                orgId = org.orgId;
                return true;
              }
            })
            App.orgId = orgId;
            App.sysUserId = userResult.data.sysUserId;
            App.isLeader = userResult.data.roleNames && userResult.data.roleNames.some(role => {
              return role.roleId === 12;
            });
            App.isSale = userResult.data.roleNames && userResult.data.roleNames.some(role => {
              return role.roleId === 4;
            });
            App.isAdmin = userResult.data.roleNames && userResult.data.roleNames.some(role => {
              return role.roleId === 1 || role.roleId === 11;
            });
            App.isFinance = userResult.data.roleNames && userResult.data.roleNames.some(role => {
              return role.roleId === 3;
            });
          }
        } else if(result.code === 30014) {
          const pages = getCurrentPages();
          const pageId = pages.pop().route;
          pageId !== 'pages/mobile-bind/main' && wx.navigateTo({
            url: '/pages/mobile-bind/main'
          })
        }else{
          wx.showToast({
            title: result.message,
            icon: 'none'
          })
        }
      }
     // await App.login();
    }
  }
</script>

<style lang="scss">
  @import "./styles/base";

</style>
